package com.projects.xenia.documents_classes;

import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

import android.content.ContentValues;
import android.database.Cursor;

import com.projects.xenia.global_modules.MainGlobalModule;
import com.projects.xenia.ref_classes.CustomersRefItem;
import com.projects.xenia.ref_classes.CustomersRefObject;
import com.projects.xenia.ref_classes.EmployeesRefItem;
import com.projects.xenia.ref_classes.EmployeesRefObject;
import com.projects.xenia.ref_classes.VisitCausesRefItem;
import com.projects.xenia.ref_classes.VisitCausesRefObject;

public class Documents_Visit {

	public DocHeader Head;
	
	public class DocHeader {
		
		public String id; 
		public int docnum; 
		public Date date_create; 
		public Date date_save; 
		public Date date_planned;
		public int state; 
		public int verstamp;		
		public EmployeesRefItem user;
		public EmployeesRefItem employee;
		public CustomersRefItem customer;
		public int visit_state;
		public VisitCausesRefItem VisitCauses;
			
	}
	
	public void NewDoc () {
		
		Head = new DocHeader ();
		
		Head.id = UUID.randomUUID().toString().toUpperCase();
		Head.docnum = GetLastDocNumByCurrentDate ();			
		Head.date_create = Calendar.getInstance().getTime();
		Head.date_save = Calendar.getInstance().getTime();
		Head.date_planned = Calendar.getInstance().getTime();
		Head.state = 0;
		Head.verstamp = 0;		
		Head.user = new EmployeesRefObject(MainGlobalModule.SettingsCollection.get("ManagerId")).RefItem;
		Head.employee = new EmployeesRefObject(MainGlobalModule.SettingsCollection.get("ManagerId")).RefItem;		
		Head.customer = null;
		Head.visit_state = 0;
		Head.VisitCauses = null;
		
	}
	
	public void OpenDoc (String id) {
		
		Head = new DocHeader ();
		
		Cursor DocumentsResult = MainGlobalModule.DBE.rawQuery("select * from xdoc_Visits_h where id = ?",new String [] {id});		
		DocumentsResult.moveToFirst();
	
		Head.id = DocumentsResult.getString(DocumentsResult.getColumnIndex("id"));
		Head.docnum = DocumentsResult.getInt(DocumentsResult.getColumnIndex("docnum"));			
		Head.date_create = MainGlobalModule.SqlStringToDate(DocumentsResult.getString(DocumentsResult.getColumnIndex("date_create")));
		Head.date_save = MainGlobalModule.SqlStringToDate(DocumentsResult.getString(DocumentsResult.getColumnIndex("date_save")));
		Head.date_planned = MainGlobalModule.SqlStringToDate(DocumentsResult.getString(DocumentsResult.getColumnIndex("date_planned")));
		Head.state = DocumentsResult.getInt(DocumentsResult.getColumnIndex("state"));
		Head.verstamp = DocumentsResult.getInt(DocumentsResult.getColumnIndex("verstamp"));
		
		Head.user = new EmployeesRefObject(DocumentsResult.getString(DocumentsResult.getColumnIndex("user_id"))).RefItem;
		Head.employee = new EmployeesRefObject(DocumentsResult.getString(DocumentsResult.getColumnIndex("employees_id"))).RefItem;
		
		Head.customer = new CustomersRefObject(DocumentsResult.getString(DocumentsResult.getColumnIndex("customers_id"))).RefItem;
		
		Head.visit_state = DocumentsResult.getInt(DocumentsResult.getColumnIndex("visit_state"));
		
		Head.VisitCauses = new VisitCausesRefObject(DocumentsResult.getString(DocumentsResult.getColumnIndex("VisitCauses_id"))).RefItem;
		
		Head.verstamp ++;
		
	}
	
	public boolean DeleteDoc () {
		
		try {
			
			Head.state = 3;
			SaveDocToDB ();
			
			return true;
			
		} catch (Exception e) {
			
			return false;
			
		}		
		
	}	
	
	public void SaveDocToDB () {
		
		ContentValues InsertUpdateValues = new ContentValues ();
		InsertUpdateValues.put("id", Head.id);
		InsertUpdateValues.put("docnum", Head.docnum);		
		InsertUpdateValues.put("date_create", MainGlobalModule.DateToSqlString(Head.date_create));
		InsertUpdateValues.put("date_save", MainGlobalModule.DateToSqlString(Head.date_save));				
		InsertUpdateValues.put("date_planned", MainGlobalModule.DateToSqlString(Head.date_planned));
		InsertUpdateValues.put("state", Head.state);
		InsertUpdateValues.put("verstamp", Head.verstamp);
		InsertUpdateValues.put("user_id", Head.user.id);
		InsertUpdateValues.put("employees_id", Head.employee.id);
		InsertUpdateValues.put("customers_id", Head.customer.id);
		
		InsertUpdateValues.put("visit_state", Head.visit_state);
		InsertUpdateValues.put("VisitCauses_id", Head.VisitCauses.id);
				
		if (MainGlobalModule.DBE.update("xdoc_Visits_h", InsertUpdateValues, "id=?", new String []{Head.id})<0) {
			MainGlobalModule.DBE.insert("xdoc_Visits_h", null, InsertUpdateValues);	
		}
		
	}
	
	public boolean CheckNewVisitsExistsByCustomerId (String customer_id) {
		
		boolean _result = false;
		
		if (customer_id!=null) {
			Cursor VisitCursor = MainGlobalModule.DBE.rawQuery("select id from xdoc_visits_h where date (date_create) = date ('now') and (visit_state = 0) and (customers_id = ?)", new String []{customer_id});
			if (VisitCursor.getCount()>0) {
				_result = true;
			}				
		}
		
		return _result;
		
	}	

	public boolean CheckOpenVisitsExists () {
		
		boolean _result = false;
		
		Cursor VisitCursor = MainGlobalModule.DBE.rawQuery("select id from xdoc_visits_h where date (date_create) = date ('now') and (visit_state = 1) and (customers_id <> ?)", new String []{Head.customer.id});
		if (VisitCursor.getCount()>0) {
			_result = true;
		}
		
		return _result;
		
	}	
	
	public boolean CheckOpenVisitsExistsByCustomerId (String customer_id) {
		
		boolean _result = false;
		
		if (customer_id!=null) {
			Cursor VisitCursor = MainGlobalModule.DBE.rawQuery("select id from xdoc_visits_h where date (date_create) = date ('now') and (visit_state = 1) and (customers_id = ?)", new String []{customer_id});
			if (VisitCursor.getCount()>0) {
				_result = true;
			}				
		}
		
		return _result;
		
	}	
	
	public boolean CheckNewOpenVisitsExistsByCustomerId (String customer_id) {
		
		boolean _result = false;
		
		if (customer_id!=null) {
			Cursor VisitCursor = MainGlobalModule.DBE.rawQuery("select id from xdoc_visits_h where date (date_create) = date ('now') and (visit_state < 2) and (customers_id = ?)", new String []{customer_id});
			if (VisitCursor.getCount()>0) {
				_result = true;
			}				
		}
		
		return _result;
		
	}	
	

	public boolean GetLastClosedVisit () {
		
		boolean _result = true;
		
		boolean VisitsSequence = Boolean.parseBoolean(MainGlobalModule.ConstantsCollection.get("VisitsSequence"));
		
		if (VisitsSequence) {
			Cursor VisitCursor = MainGlobalModule.DBE.rawQuery("select max (docnum) from xdoc_visits_h where date (date_create) = date ('now') and (visit_state > 1)", null);			
			if (VisitCursor.getCount()>0) {
				VisitCursor.moveToFirst();
				int lastdocvisit = VisitCursor.getInt(0);
				if (Head.docnum != (lastdocvisit+1)) {
					_result = false; 
				}
			}	
		}
		
		return _result;
		
	}	
	
	public boolean CheckOrderExists () {
		
		boolean _result = false;
		
		Cursor VisitCursor = MainGlobalModule.DBE.rawQuery("select id from xdoc_orders_h where date (date_create) = date ('now') and customers_id = ? and total_ordersum > 0", new String [] {Head.customer.id});
		
		if (VisitCursor.getCount()>0) {
			_result = true;
		}
		
		return _result;
		
	}
	
	
	
	private int GetLastDocNumByCurrentDate () {

		int DocNum = 0;
		Cursor ScalarResult = MainGlobalModule.DBE.rawQuery("select max(docnum) from xdoc_visits_h", null);

		if (ScalarResult.getCount()>0) {
			ScalarResult.moveToFirst();
			DocNum = ScalarResult.getInt(0);								
		}

		ScalarResult.close();

		DocNum ++;

		return (DocNum);

	}	
	
}
